//
//  LSMainViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2018/12/18.
//  Copyright © 2018 wpkj. All rights reserved.
//

#import "LSMainViewController.h"
#import "LSMainButtonView.h"
#import "LSLoginViewController.h"
#import "PBMineButton.h"

#import "LSInfoCollectViewController.h"//信息采集控制器
#import "LSDamageReportViewController.h"//灾情上报控制器
#import "LSDataCollectViewController.h"//数据采集控制器
#import "LSExamineApproveViewController.h"//查阅审批控制器
#import "LSOAViewController.h"//移动OA控制器
#import "LSNotiViewController.h"//通知公告
#import "LSNewsViewController.h"//新闻动态
#import "LSPolicyLawsViewController.h"//政策法规
#import "LSAttendanceManagementController.h"//考勤管理
#import "LSMeassgeCenterViewController.h"//消息中心控制器

@interface LSMainViewController ()
@property (nonatomic, strong) NSArray <NSString *> *buttonsArray;
@end

@implementation LSMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self presentLoginVC];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentLoginVC) name:UINotificationGoLoginMessageName object:nil];
    [self setUpFakeNavigationBar];
    [self setUpBannerView];
    [self setUpButtons];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self getMessageCount];
}
- (void)presentLoginVC {
    LSLoginViewController *loginVC = [[LSLoginViewController alloc] init];
    HEBaseNavController *loginNav = [[HEBaseNavController alloc] initWithRootViewController:loginVC];
    loginNav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:loginNav animated:false completion:nil];
}

- (void)setUpFakeNavigationBar {
    UIView *fakeNavigationBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SafeAreaTopHeight)];
    fakeNavigationBarView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:fakeNavigationBarView];
    UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SafeAreaTopHeight-44)];
    statusBarView.backgroundColor = NAV_COLOR;
    [fakeNavigationBarView addSubview:statusBarView];
    UILabel *mainTitleLabel = [[UILabel alloc] init];
    mainTitleLabel.text = @"首页";
    mainTitleLabel.textColor = hexColor(666666);
    mainTitleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:17];
    [mainTitleLabel sizeToFit];
    mainTitleLabel.center = CGPointMake(ScreenWidth/2, 22+(SafeAreaTopHeight-44));
    [fakeNavigationBarView addSubview:mainTitleLabel];
    PBMineButton *noticeButton = [[PBMineButton alloc] initWithFrame:CGRectMake(ScreenWidth-44-15, SafeAreaTopHeight-44, 44, 44)];
    [noticeButton setImage:[UIImage imageNamed:@"noticeCenter"] forState:UIControlStateNormal];
    noticeButton.tag = 10000;
    [noticeButton addTarget:self action:@selector(noticeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [fakeNavigationBarView addSubview:noticeButton];
}
- (void)noticeButtonClick {
    LSMeassgeCenterViewController *meassgeCenterVC = [[LSMeassgeCenterViewController alloc] init];
    [self.navigationController pushViewController:meassgeCenterVC animated:true];
}
- (void)getMessageCount{
    if (!([[ValidateUtils validateNull:[UserInfoModel sharedInstance].token] isEqualToString:@""] || ![UserInfoModel sharedInstance].isLogin)) {
        [NetworkUtils requestNOHandleWithMethod:@"POST" URL:cxxxForApp param:@{@"token":[UserInfoModel sharedInstance].token} completionHandler:^(id responseObject, NSError *connectionError) {
            if (!connectionError) {
                NSNumber *code = responseObject[@"status"];
                if (code.integerValue == 200) {
                    NSArray *arr = [NSArray arrayWithArray:responseObject[@"data"]];
                    if (arr.count>0) {
                        NSDictionary *dict=  arr.firstObject;
                        if ([ValidateUtils validateNull:dict[@"count"]].integerValue > 0) {
                            PBMineButton *btn = [self.view viewWithTag:10000];
                            btn.taggerNumber = [[ValidateUtils validateNull:dict[@"count"]] intValue];
                        }
                    }
                }
            }
        }];
    }
}
- (void)setUpBannerView {
    UIImageView *bannerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, ScreenWidth, kHeight(150))];
    bannerView.image = [UIImage imageNamed:@"banner"];
    [self.view addSubview:bannerView];
}

- (void)setUpButtons {
    for (int i = 0; i < self.buttonsArray.count; i++) {
        NSInteger index = i % 3;
        NSInteger page = i / 3;
        LSMainButtonView *button = [[LSMainButtonView alloc] initWithFrame:CGRectMake((ScreenWidth-8)/3*index+4, SafeAreaTopHeight+kHeight(150)+130*page, (ScreenWidth-8)/3, 130)];
        button.tag = 2018+i;
        button.buttonTitle = self.buttonsArray[i];
        button.buttonImageName = self.buttonsArray[i];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mainButtonViewTapped:)];
        [button addGestureRecognizer:tap];
        [self.view addSubview:button];
    }
}

- (void)mainButtonViewTapped:(UITapGestureRecognizer *)tap {
    LSMainButtonView *button = (LSMainButtonView *)tap.view;
    NSInteger tag = button.tag-2018;
    switch (tag) {
        case 0:
            [self.navigationController pushViewController:[[LSInfoCollectViewController alloc] init] animated:true];
            break;
        case 1:
//            [self.navigationController pushViewController:[[LSDamageReportViewController alloc] init] animated:true];
            [self.navigationController pushViewController:[[LSDataCollectViewController alloc] init] animated:true];
            break;
        case 2:
//            [self.navigationController pushViewController:[[LSDataCollectViewController alloc] init] animated:true];
        {
            // 考勤管理
            LSAttendanceManagementController *attendanceManagementVC = [[LSAttendanceManagementController alloc] init];
            [self.navigationController pushViewController:attendanceManagementVC animated:true];
        }
            break;
        case 3: {
            // 通知公告
            LSNotiViewController *notiVC = [[LSNotiViewController alloc] initWithTabbar:false];
            [self.navigationController pushViewController:notiVC animated:true];
        }
            break;
        case 4: {
            // 拉萨要闻
            LSNewsViewController *newsVC = [[LSNewsViewController alloc] init];
            [self.navigationController pushViewController:newsVC animated:true];
        }
            break;
        case 5: {
            // 政策解读
            LSPolicyLawsViewController *policyLawsVC = [[LSPolicyLawsViewController alloc] init];
            [self.navigationController pushViewController:policyLawsVC animated:true];
        }
            break;
        default:
            break;
    }
}

- (NSArray<NSString *> *)buttonsArray {
    if (!_buttonsArray) {
        _buttonsArray = @[@"信息采集",@"数据管理",@"移动OA",@"通知公告",@"拉萨要闻",@"政策解读"];
    }
    return _buttonsArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
